package com.ly.emp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.common.core.utils.excel.ExcelResult;
import com.ly.common.core.utils.excel.ExcelUtil;
import com.ly.emp.entity.HubBaseConfig;
import com.ly.emp.service.IHubBaseConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 班组基础信息配置
 *
 * @author luoyun
 * @Date: 2025-07-16
 */
@Tag(name="班组基础信息配置")
@Slf4j
@RestController
@RequestMapping("/emp/hubBaseConfig")
@RequiredArgsConstructor
public class HubBaseConfigController {

	private final IHubBaseConfigService hubBaseConfigService;

	/**
     * 查询-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询-id")
    @GetMapping("/getId")
    public R<HubBaseConfig> getId(@RequestParam(name="id") String id) {
        return hubBaseConfigService.getId(id);
    }

	/**
	 * 查询-code
	 *
	 * @param orgCode
	 * @return
	 */
	@Operation(summary = "查询-code")
	@GetMapping("/getByOrgCode")
	public R<HubBaseConfig> getByOrgCode(@RequestParam(name="orgCode") String orgCode) {
		HubBaseConfig one = hubBaseConfigService.getOne(
				new LambdaQueryWrapper<HubBaseConfig>()
						.eq(HubBaseConfig::getOrgCode,orgCode)
		);
		return R.ok(one);
	}

	 /**
	  * 列表
	  * @param entity
	  * @return
	  */
	 @Operation(summary="列表")
	 @GetMapping("/getList")
	 public R<List<HubBaseConfig>> getList(HubBaseConfig entity) {
		 return hubBaseConfigService.getList(entity);
	 }
	
	/**
     * 分页
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
	@Operation(summary="分页")
	@GetMapping("/getPage")
	public R<IPage<HubBaseConfig>> getPage(
	        HubBaseConfig entity,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		return hubBaseConfigService.getPage(entity,new Page<HubBaseConfig>(pageNo, pageSize));
	}
	
	/**
	 * 新增
	 *
	 * @param entity
	 * @return
	 */
	@Operation(summary = "新增")
    @PostMapping("/add")
	public R<String> add(@RequestBody HubBaseConfig entity) {
	    return hubBaseConfigService.add(entity);
	}
	
	/**
	 * 修改
	 *
	 * @param entity
	 * @return
	 */
	@Operation(summary = "修改")
    @PostMapping("/edit")
	public R<String> edit(@RequestBody HubBaseConfig entity) {
		return hubBaseConfigService.edit(entity);
	}
	
	/**
	 * 删除-id
	 *
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除-id")
    @DeleteMapping("/delete")
	public R<String> delete(@RequestParam(name="id") String id) {
		return hubBaseConfigService.delete(id);
	}
	
	/**
     * 批量新增或修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "批量新增或修改")
    @PostMapping("/updateBatch")
    public R updateBatch(@Validated @RequestBody CommonBatchUpdateDTO<HubBaseConfig> dto) {
        return hubBaseConfigService.updateBatch(dto);
    }
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@Operation(summary="批量删除")
	@DeleteMapping("/deleteBatch")
	public R<String> deleteBatch(@RequestParam(name="ids") String ids) {
		return hubBaseConfigService.deleteBatch(ids);
	}

    /**
     * 导出excel
     *
     * @param response
     */
    @Operation(summary = "导出excel")
    @GetMapping("/exportXls")
    public void exportXls(HttpServletResponse response) {
        List<HubBaseConfig> list = hubBaseConfigService.list();
        ExcelUtil.exportExcel(list, "班组基础信息配置表", HubBaseConfig.class, response);
    }

    /**
     * 导入excel
     *
     * @param file
     * @param updateSupport：是否删除标识
     * @return
     */
    @Operation(summary = "导入excel")
    @PostMapping("/importExcel")
    public R<?> importWork(MultipartFile file, boolean updateSupport) throws Exception {
        // 监听： new ImportListener(updateSupport);
        ExcelResult<HubBaseConfig> result = ExcelUtil.importExcel(file.getInputStream(), HubBaseConfig.class, null);
        return R.ok(true, result.getAnalysis());
    }

}
